<script lang="ts" setup>
import { inject, nextTick, reactive, ref, provide } from "vue";
import commentInput from "@/components/funcs/commentInput/commentInput.vue";
const getChildrenComments = inject<
  (parentId: number, page: number, pageSize: number) => Promise<any>
>("getChildrenComments", () => Promise.resolve());
const props = defineProps({
  data: {
    type: Object,
    default: () => {},
  },
});
//---
const totalChildren = ref<number>(0);
totalChildren.value = props.data.totalReplies;
const isGetting = ref<boolean>(false);
const childrenComments = ref<any[]>([]);
const pageing: {
  currentPage: number;
  totalPage: number;
  pageSize: number;
  page: number;
} = reactive({
  currentPage: 1,
  totalPage: 1,
  pageSize: 2,
  page: 1,
});
const getChildren = async (id: number): Promise<void> => {
  isGetting.value = true;
  const res = await getChildrenComments!(id, pageing.page++, pageing.pageSize);
  if (res) {
    totalChildren.value = res.total;
    childrenComments.value.push(...res.comments);
    pageing.currentPage = res.currentPage;
    pageing.totalPage = res.totalPage;
    nextTick(() => {
      isGetting.value = false;
    });
  }
};
const refreshChildrenComments = (): void => {
  noShow();
  getChildren(props.data.id);
};
const noShow = (): void => {
  childrenComments.value = [];
  pageing.currentPage = 1;
  pageing.totalPage = 1;
  pageing.page = 1;
};
provide("refreshChildrenComments", refreshChildrenComments);
//---
const showReplyInput = ref<boolean>(false);
</script>
<template>
  <div class="item">
    <div style="display: flex">
      <div class="conAvatar">
        <img :src="data.User.avatarUrl" alt="" />
      </div>
      <div class="conRight">
        <div class="userName">{{ data.User.username }}</div>
        <div class="content">{{ data.content }}</div>
        <div class="tag">
          <div class="time">{{ data.date }}</div>
          <div class="bthReply" @click="showReplyInput = !showReplyInput">
            <el-icon><ChatRound /></el-icon
            >{{ showReplyInput ? "收起" : "回复" }}
          </div>
        </div>
      </div>
    </div>
    <div class="conChildren" v-if="childrenComments.length">
      <div class="cItem" v-for="item in childrenComments" :key="item.id">
        <div class="conCAvatar">
          <img :src="item.User.avatarUrl" alt="" />
        </div>
        <div class="conCright">
          <div class="cUserName">{{ item.User.username }}</div>
          <div class="cContent">
            {{ item.content }}
          </div>
          <div class="cTime">{{ item.date }}</div>
        </div>
      </div>
    </div>
    <div class="maskLoad" v-if="isGetting">
      加载中<el-icon><Loading /></el-icon>
    </div>
    <div
      class="showChildren"
      v-else-if="totalChildren && !isGetting && !childrenComments.length"
      @click="getChildren(data.id)"
    >
      — 展开{{ totalChildren }}条回复
    </div>
    <div
      class="showMore"
      v-else-if="
        !isGetting &&
        childrenComments.length &&
        pageing.currentPage < pageing.totalPage
      "
    >
      <span @click="getChildren(data.id)"
        > — 更多<el-icon><ArrowDown /></el-icon
      ></span>
      <span @click="noShow"
        >收起<el-icon><ArrowUp /></el-icon
      ></span>
    </div>
    <div
      class="noShow"
      v-else-if="
        !isGetting &&
        childrenComments.length &&
        pageing.currentPage >= pageing.totalPage
      "
    >
      <span @click="noShow"
        >收起<el-icon><ArrowUp /></el-icon
      ></span>
    </div>
  </div>
  <commentInput
    :parentId="data.id"
    :placeholder="`回复${data.User.username}:`"
    v-if="showReplyInput"
  />
</template>
<style lang="scss" scoped>
.item {
  width: 100%;
  border-bottom: 1px solid #eee;
  padding-bottom: 20px;
  .conAvatar {
    width: 40px;
    height: 40px;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
  .conRight {
    margin-left: 10px;
    width: 75%;
    .userName {
      font-size: 13px;
      font-weight: 550;
      color: #333;
      max-width: 80%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    .content {
      font-size: 14px;
      line-height: 24px;
      color: black;
      margin-top: 10px;
      max-width: 50%;
    }
    .tag {
      display: flex;
      font-size: 12px;
      padding-left: 10px;
      margin-top: 10px;
      .time {
        margin-right: 20px;
      }
      .bthReply {
        cursor: pointer;
        width: 40px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        &:hover {
          color: #409eff;
        }
      }
    }
  }
  .conChildren {
    margin-top: 10px;
    width: 90%;
    margin-left: 10%;
    .cItem {
      display: flex;
      width: 100%;
      margin: 25px 0;
      .conCAvatar {
        width: 30px;
        height: 30px;
        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
        }
      }
      .conCright {
        width: 70%;
        margin-left: 10px;
        .cUserName {
          font-size: 13px;
          font-weight: 600;
          color: #333;
          max-width: 80%;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
        .cContent {
          font-size: 14px;
          color: black;
          margin-top: 10px;
        }
        .cTime {
          font-size: 12px;
          margin-top: 8px;
          margin-left: 10px;
        }
      }
    }
  }
  .unShow {
    width: 50%;
    margin-left: 13%;
    cursor: pointer;
    &:hover {
      color: #409eff;
    }
  }
  .continueShow {
    width: 50%;
    margin-left: 13%;
    display: flex;
    justify-content: space-around;
    div {
      cursor: pointer;
      &:hover {
        color: #409eff;
      }
    }
  }
  .maskLoad {
    margin-top: 15px;
    font-size: 13px;
    margin-bottom: 2px;
    text-align: center;
    transition: all 0.2s;
  }
  .showChildren {
    margin-top: 15px;
    font-size: 15px;
    cursor: pointer;
    transition: all 0.2s;
    text-indent: 2rem;
    &:hover {
      color: #409eff;
    }
  }
  .showMore {
    margin-top: 16px;
    font-size: 14px;
    display: flex;
    justify-content: space-around;
    width: 60%;

    span {
      cursor: pointer;
      transition: all 0.2s;
      display: flex;
      align-items: center;
      &:hover {
        color: #409eff;
      }
    }
  }
  .noShow {
    margin-top: 16px;
    font-size: 14px;
    margin-left: 30px;
    span {
      cursor: pointer;
      transition: all 0.2s;
      &:hover {
        color: #409eff;
      }
    }
  }
}
</style>
